package com.whpost.service.service.stamp.h5;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.config.Wdpz;
import com.whpost.service.dao.addr.TWxAddrDao;
import com.whpost.service.dao.addr.TWxAddressBookDao;
import com.whpost.service.dao.stamp.*;
import com.whpost.service.dao.t.MyDao;
import com.whpost.service.dao.t.TDelDao;
import com.whpost.service.dao.t.TWxInfoDao;
import com.whpost.service.dao.wx.TWxOrderDao;
import com.whpost.service.dao.wx.TWxUserDao;
import com.whpost.service.entity.addr.TWxAddressBook;
import com.whpost.service.entity.addr.TWxProvince;
import com.whpost.service.entity.stamp.*;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.t.TDel;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.entity.wx.TWxInfo;
import com.whpost.service.entity.wx.TWxOrder;
import com.whpost.service.entity.wx.TWxUser;
import com.whpost.service.service.BaseServices;
import com.whpost.service.service.H5BaseServices;
import com.whpost.service.service.stamp.StampCardOrderServices;
import com.whpost.service.service.stamp.StampOrderServices;
import com.whpost.service.service.t.FileService;
import com.whpost.service.service.t.TApiNonceH5Service;
import com.whpost.service.service.t.TApiNonceService;
import com.whpost.service.tools.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class StampH5Services extends H5BaseServices {

    @Resource
    private StampXmMapper stampXmMapper;
    @Resource
    private StampHdMapper stampHdMapper;
    @Resource
    private StampHdMbMapper stampHdMbMapper;
    @Resource
    private TApiNonceH5Service tApiNonceH5Service;
    @Resource
    private StampCardMapper stampCardMapper;
    @Resource
    private StampCardOrderMapper stampCardOrderMapper;
    @Resource
    private StampCardOrderMxMapper stampCardOrderMxMapper;
    @Resource
    private TWxAddressBookDao tWxAddressBookDao;
    @Resource
    private TWxAddrDao tWxAddrDao;
    @Resource
    private TWxOrderDao tWxOrderDao;
    @Resource
    private TWxInfoDao tWxInfoDao;
    @Resource
    private MyDao myDao;
    @Resource
    private Wdpz wdpz;
    @Resource
    private TWxUserDao tWxUserDao;
    @Resource
    private WxPay wxPay;
    @Resource
    private StampOrderServices stampOrderServices;

    public ResultInfo getHd(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String xm_id="";
            Map<String,Object> infoMap = new HashMap<>();
            if (jsonObject.containsKey("xm_id")) {
                xm_id = jsonObject.getString("xm_id");
            }
            if(StringUtils.isEmpty(xm_id)){
                return new ResultInfo(500,"项目信息丢失！",null);
            }
            try {
                xm_id = Url.decrypt(xm_id);
            } catch (Exception e) {
                throw new RuntimeException("项目信息丢失！");
            }
            StampXm stampXm = stampXmMapper.selectById(xm_id);
            List<StampHd> stampHdList = new ArrayList<StampHd>();
            stampHdList = stampHdMapper.selectList(new QueryWrapper<StampHd>().eq("xm_id",xm_id).eq("sfsyxs","1").orderByAsc("xsxh"));

            if (null!=stampXm){
                stampXm.setUrlId(ToolBean.getEncode(stampXm.getId()));
                stampXm.setId(null);
            }

            // 循环遍历 List 并改变每个对象的 age 属性
            for (StampHd stampHd : stampHdList) {
                stampHd.setUrlId(ToolBean.getEncode(stampHd.getId()));
                stampHd.setId(null);
                stampHd.setUrlXmId(ToolBean.getEncode(stampHd.getXmId()));
                stampHd.setXmId(null);
            }

            infoMap.put("stampXm",stampXm);
            infoMap.put("stampHdList",stampHdList);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo getHdMb(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String hd_id="";
            Map<String,Object> infoMap = new HashMap<>();
            if (jsonObject.containsKey("hd_id")) {
                hd_id = jsonObject.getString("hd_id");
            }
            if(StringUtils.isEmpty(hd_id)){
                return new ResultInfo(500,"活动信息丢失！",null);
            }
            try {
                hd_id = Url.decrypt(hd_id);
            } catch (Exception e) {
                throw new RuntimeException("活动信息丢失！");
            }
            StampHd stampHd = stampHdMapper.selectById(hd_id);

            if (null!=stampHd){
                stampHd.setUrlId(ToolBean.getEncode(stampHd.getId()));
                stampHd.setId(null);
                stampHd.setUrlXmId(ToolBean.getEncode(stampHd.getXmId()));
                stampHd.setXmId(null);
            }
            List<StampHdMb> stampHdMbList1 = new ArrayList<StampHdMb>();
            List<StampHdMb> stampHdMbList2 = new ArrayList<StampHdMb>();
            stampHdMbList1 = stampHdMbMapper.getMbList(hd_id,"1");
            stampHdMbList2 = stampHdMbMapper.getMbList(hd_id,"2");
            // 循环遍历 List 并改变每个对象的 age 属性
            for (StampHdMb stampHdMb : stampHdMbList1) {
                stampHdMb.setUrlId(ToolBean.getEncode(stampHdMb.getId()));
            }
            // 循环遍历 List 并改变每个对象的 age 属性
            for (StampHdMb stampHdMb : stampHdMbList2) {
                stampHdMb.setUrlId(ToolBean.getEncode(stampHdMb.getId()));
            }
            infoMap.put("stampHd",stampHd);
            infoMap.put("stampHdMbList1",stampHdMbList1);
            infoMap.put("stampHdMbList2",stampHdMbList2);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo getHdGxhMb(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String hd_id="";
            Map<String,Object> infoMap = new HashMap<>();
            if (jsonObject.containsKey("hd_id")) {
                hd_id = jsonObject.getString("hd_id");
            }
            if(StringUtils.isEmpty(hd_id)){
                return new ResultInfo(500,"活动信息丢失！",null);
            }
            try {
                hd_id = Url.decrypt(hd_id);
            } catch (Exception e) {
                throw new RuntimeException("活动信息丢失！");
            }
            StampHd stampHd = stampHdMapper.selectById(hd_id);

            if (null!=stampHd){
                stampHd.setUrlId(ToolBean.getEncode(stampHd.getId()));
                stampHd.setId(null);
                stampHd.setUrlXmId(ToolBean.getEncode(stampHd.getXmId()));
                stampHd.setXmId(null);
            }
            infoMap.put("stampHd",stampHd);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public String qrta(String loginId, String imgDate, String lx, String stmbid, String htmbid, String hdid) {
        try {
            if (!StringUtils.isEmpty(hdid)){
                hdid = Url.decrypt(hdid);
            }
            String mbid = "";
            if (lx.equals("1")){
                mbid = stmbid;
            }
            else if (lx.equals("2")){
                mbid = htmbid;
            }
            if (StringUtils.isEmpty(hdid)){
                return "fail|合成图片失败,活动信息丢失！";
            }

            if (StringUtils.isEmpty(mbid)){
                return "fail|合成图片失败,模板丢失！";
            }

            if (StringUtils.isEmpty(loginId)){
                return "fail|合成图片失败,用户信息丢失！";
            }

            if (StringUtils.isEmpty(hdid)){
                return "fail|合成图片失败,活动信息丢失！";
            }

            //判断操作系统
            String os = System.getProperty("os.name");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String res = sdf.format(new Date());
            String newFileName = res + UUID.randomUUID().toString();

            //图片路径
            String picPathReal="";
            if(os!=null&&os.contains("Windows")){
                picPathReal="d:\\stamp\\card\\"+loginId+"\\";
            }else{
                //linux
                picPathReal="/home/stamp/card/"+loginId+"/";
            }

            File tempFile = new File(picPathReal);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }

            //先保存上传图片
            String yhtp = picPathReal+newFileName+".jpg";
            ToolBean.info("准备合成图片>>>保存用户上传图片["+yhtp+"]>>开始");
            FileUtil.base64ToImgTransformation(yhtp,imgDate);
            ToolBean.info("准备合成图片>>>保存用户上传图片["+yhtp+"]>>结束");

            StampHd stampHd = stampHdMapper.selectById(hdid);

            if ("0".equals(mbid)){
                ToolBean.info("准备合成图片>>>["+yhtp+"]无背景");
                String hclj = picPathReal+newFileName+"_hctp.jpg";
                String hclj_zst = picPathReal+newFileName+"_hctp_zst.jpg";
                if (lx.equals("1"))
                {
                    FileUtil.wbj_hctp(yhtp,hclj);
                    FileUtil.wbj_hctp_zst(yhtp,hclj_zst);
                }
                else if (lx.equals("2"))
                {
                    FileUtil.wbj_hctp1(yhtp,hclj);
                    FileUtil.wbj_hctp1_zst(yhtp,hclj_zst);
                }
                //写订单信息
                StampCard stampCard = new StampCard();
                stampCard.setXmId(stampHd.getXmId());
                stampCard.setHdId(stampHd.getId());
                stampCard.setTHyxxId(Long.valueOf(loginId));
                stampCard.setLx(lx);
                stampCard.setWjlj(hclj);
                stampCard.setWjljZs(hclj_zst);
                stampCardMapper.insert(stampCard);

                return "suc|"+ToolBean.getEncode(stampCard.getId());
            }else{
                //找到模板图片，需要判断模版是否已经删除了，以后再加这个逻辑！
                StampHdMb stampHdMb = new StampHdMb();
                stampHdMb = stampHdMbMapper.selectById(mbid);
                String wjm = stampHdMb.getMblj();
                String wjm_slt = stampHdMb.getMblj1();
                String mblj = "";
                String mblj_slt = "";
                if(os!=null&&os.contains("Windows")){
                    //System.out.println("Windows");
                    mblj="d:\\stamp\\mb\\"+lx+"\\"+wjm;
                    mblj_slt="d:\\stamp\\mb\\"+lx+"\\"+wjm_slt;
                }else{
                    //linux
                    mblj="/home/stamp/mb/"+lx+"/"+wjm;
                    mblj_slt="/home/stamp/mb/"+lx+"/"+wjm_slt;
                }
                //开始合成
                ToolBean.info("准备合成图片>>>路径1["+yhtp+"]>>>路径2["+mblj+"]");
                String hclj = picPathReal+newFileName+"_hctp.jpg";
                String hclj_zst = picPathReal+newFileName+"_hctp_zst.jpg";
                if (lx.equals("1"))
                {
                    FileUtil.hctp(yhtp,mblj,hclj);
                    FileUtil.hctp_zst(yhtp,mblj_slt,hclj_zst);
                }
                else if (lx.equals("2"))
                {
                    FileUtil.hctp1(yhtp,mblj,hclj);
                    FileUtil.hctp1_zst(yhtp,mblj_slt,hclj_zst);
                }
                //写订单信息
                StampCard stampCard = new StampCard();
                stampCard.setXmId(stampHd.getXmId());
                stampCard.setHdId(stampHd.getId());
                stampCard.setTHyxxId(Long.valueOf(loginId));
                stampCard.setLx(lx);
                stampCard.setWjlj(hclj);
                stampCard.setWjljZs(hclj_zst);
                stampCardMapper.insert(stampCard);

                return "suc|"+ToolBean.getEncode(stampCard.getId());
            }


        } catch (Exception e) {
            e.printStackTrace();
            ToolBean.info("合成图片失败["+e.toString()+"]");
            return "fail|合成图片失败";
        }
    }

    public ResultInfo getWdzp(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String hd_id="",xm_id="";
            Map<String,Object> infoMap = new HashMap<>();
            if (jsonObject.containsKey("hd_id")) {
                hd_id = jsonObject.getString("hd_id");
            }
            if (jsonObject.containsKey("xm_id")) {
                xm_id = jsonObject.getString("xm_id");
            }
            if(StringUtils.isEmpty(xm_id)){
                return new ResultInfo(500,"活动信息丢失！",null);
            }
            try {
                if (!StringUtils.isEmpty(hd_id)) {
                    hd_id = Url.decrypt(hd_id);
                }
            } catch (Exception e) {
                hd_id="";
            }
            try {
                if (!StringUtils.isEmpty(xm_id)) {
                    xm_id = Url.decrypt(xm_id);
                }
            } catch (Exception e) {
                xm_id="";
            }

            //该项目底下所有活动
            List<StampHd> stampHdList = new ArrayList<>();
            stampHdList = stampHdMapper.selectList(new QueryWrapper<StampHd>().eq("xm_id",xm_id));

            if (null==stampHdList){
                return new ResultInfo(500,"没有获取到活动信息！",null);
            }

            if (stampHdList.size()==0){
                return new ResultInfo(500,"没有获取到活动信息！",null);
            }

            // 循环遍历 List
            for (StampHd stampHd : stampHdList) {
                stampHd.setUrlId(ToolBean.getEncode(stampHd.getId()));
                stampHd.setId(null);
                stampHd.setUrlXmId(ToolBean.getEncode(stampHd.getXmId()));
                stampHd.setXmId(null);
            }

            ToolBean.info("hd_id:"+hd_id);
            //ToolBean.info("stampHdList:"+stampHdList.size());

            infoMap.put("stampHdList",stampHdList);

            List<StampCard> stampCardList = new ArrayList<StampCard>();
            if (!StringUtils.isEmpty(hd_id)){
                stampCardList = stampCardMapper.getStampCardList(new QueryWrapper<StampCard>().eq("t_hyxx_id",loginId).eq("hd_id",hd_id).eq("scbz","0").orderByDesc("id"));

                infoMap.put("hd_id",ToolBean.getEncode(hd_id)); //传过来的hd_id，前台需要匹配select选中
                infoMap.put("hd_lx",stampHdMapper.selectById(hd_id).getLxId()); //传过来的hd_id，前台需要匹配select选中
                infoMap.put("zdxdsl",stampHdMapper.selectById(hd_id).getZdxdsl()); //传过来的hd_id，前台需要匹配select选中
                infoMap.put("stampCardList",stampCardList);
            }else{
                //如果没有hd_id，默认取第一个，保证一定要有活动id，暂时不支持活动混合下单
                String hd_id1=stampHdList.get(0).getUrlId();
                stampCardList = stampCardMapper.getStampCardList(new QueryWrapper<StampCard>().eq("t_hyxx_id",loginId).eq("hd_id",ToolBean.getDecodeWithNetDecode(hd_id1)).eq("scbz","0").orderByDesc("id"));
                ToolBean.info("stampCardList:"+stampCardList.size());
                infoMap.put("hd_id",hd_id1); //传过来的hd_id，前台需要匹配select选中
                infoMap.put("hd_lx",stampHdMapper.selectById(ToolBean.getDecodeWithNetDecode(hd_id1)).getLxId()); //传过来的hd_id，前台需要匹配select选中
                infoMap.put("zdxdsl",stampHdMapper.selectById(ToolBean.getDecodeWithNetDecode(hd_id1)).getZdxdsl()); //传过来的hd_id，前台需要匹配select选中
                infoMap.put("stampCardList",stampCardList);
            }
            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo wdzpDel(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String id="";
            Map<String,Object> infoMap = new HashMap<>();
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            ToolBean.info("准备删除："+id);

            StampCard stampCard = stampCardMapper.selectById(id);
            if (stampCard!=null){
                if (stampCard.getTHyxxId().equals(Long.parseLong(loginId))){
                    //直接打标，不删除！
                    stampCardMapper.updateScbz1(id);
                    ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
                    return backRtn(resultInfo,loginId);
                }else{
                    //直接打标，不删除！
                    ResultInfo resultInfo = new ResultInfo(500, "不允许删除别人的照片！",infoMap);
                    return backRtn(resultInfo,loginId);
                }

            }else{
                ResultInfo resultInfo = new ResultInfo(500, "照片不存在！",infoMap);
                return backRtn(resultInfo,loginId);
            }
        }
    }

    @Transactional
    public ResultInfo createOrder(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String ids="",sls="",hd_id="",nonce_ms="",nonce_zhi="";
            Map<String,Object> infoMap = new HashMap<>();
            if (jsonObject.containsKey("ids")) {
                ids = jsonObject.getString("ids");
            }
            if (jsonObject.containsKey("sls")) {
                sls = jsonObject.getString("sls");
            }
            if (jsonObject.containsKey("hd_id")) {
                hd_id = jsonObject.getString("hd_id");
            }
            if (!StringUtils.isEmpty(hd_id)) {
                try {
                    hd_id = ToolBean.getDecodeWithNetDecode(hd_id);
                } catch (Exception e) {
                    return new ResultInfo(500,"活动信息丢失！",null);
                }
            }

            StampHd stampHd = stampHdMapper.selectById(hd_id);
            if (null==stampHd){
                return new ResultInfo(500,"活动信息丢失！",null);
            }

            String hdlx = String.valueOf(stampHd.getLxId());

            int zdsl = stampHd.getZdxdsl();

            ToolBean.info("活动id["+hd_id+"]开始制作会员["+loginId+"]的产品...开始");
            List<String> mbid = JSON.parseArray(ids,String.class);
            List<String> sl = JSON.parseArray(sls,String.class);
            if (null==mbid || null==sl){
                return new ResultInfo(500,"产品信息丢失！",null);
            }

            if (jsonObject.containsKey("nonce_ms")) {
                nonce_ms = jsonObject.getString("nonce_ms");
            }
            if (jsonObject.containsKey("nonce_zhi")) {
                nonce_zhi = jsonObject.getString("nonce_zhi");
            }

            //校验nonce
            ResultInfo ResultInfoCheckNonce = new ResultInfo();
            ResultInfoCheckNonce = tApiNonceH5Service.checkNonce(nonce_ms,loginId,nonce_zhi,"stampxm.save");

            if (ResultInfoCheckNonce.getCode()!=200)
            {
                return ResultInfoCheckNonce;
            }

            //开始写逻辑
            Double zsl =0.0; //总数量
            for (int k=0;k<sl.size();k++){
                zsl+=Double.parseDouble(sl.get(k));
            }

            if (zsl>zdsl){
                throw new RuntimeException("订单生成错误！该活动最大数量限制为["+zdsl+"]张！");
            }

            //写订单主表
            StampCardOrder stampCardOrder = new StampCardOrder();
            stampCardOrder.setHdId(Long.valueOf(hd_id));
            stampCardOrder.setTHyxxId(Long.valueOf(loginId));
            stampCardOrder.setZsl(Double.parseDouble(String.valueOf(zsl)));
            stampCardOrder.setLqfs("1");
            stampCardOrder.setCzrq(new Date());
            stampCardOrderMapper.insert(stampCardOrder);

            ToolBean.info("活动id["+hd_id+"]开始制作会员["+loginId+"]的产品...开始>>>订单主表写完！");

            //获取单价、邮费、免运费标准
            String dj = String.valueOf(stampHd.getJg());
            String yf = String.valueOf(stampHd.getYf());
            String bysl = String.valueOf(stampHd.getBysl());

            //明信片的总价格
            String jg = "0";
            //写订单明细表
            for (int k=0;k<mbid.size();k++) {

                ToolBean.info("活动id["+hd_id+"]开始制作会员["+loginId+"]的产品...开始>>>准备写明细表...");

                StampCard stampCard = new StampCard();
                stampCard = stampCardMapper.selectById(mbid.get(k));
                if (null==stampCard){
                    throw new RuntimeException("没有找到照片！");
                }
                if (!stampCard.getTHyxxId().equals(Long.parseLong(loginId))){
                    throw new RuntimeException("非自己的照片！");
                }

                ToolBean.info("活动id["+hd_id+"]开始制作会员["+loginId+"]的产品...开始>>>准备写明细表...，获取到的stampcard为["+stampCard.getId()+"]");

                if (hdlx.equals("1")){
                    if (stampCard.getLx().equals("3")){
                        throw new RuntimeException("订单生成错误！活动类型和照片不符！请重新勾选需要制作的照片！");
                    }
                }

                if (hdlx.equals("2")){
                    if (!stampCard.getLx().equals("3")){
                        throw new RuntimeException("订单生成错误！活动类型和照片不符！请重新勾选需要制作的照片！");
                    }
                }

                StampCardOrderMx stampCardOrderMx = new StampCardOrderMx();
                stampCardOrderMx.setStampCardOrderId(stampCardOrder.getId());
                stampCardOrderMx.setStampCardId(Long.valueOf(mbid.get(k)));
                stampCardOrderMx.setTHyxxId(Long.valueOf(loginId));
                stampCardOrderMx.setSl(Integer.parseInt(sl.get(k)));
                stampCardOrderMx.setDj(Double.parseDouble(dj));
                String hj = String.valueOf(Double.parseDouble(sl.get(k))*Double.parseDouble(dj));
                jg = String.valueOf(Double.parseDouble(jg)+Double.parseDouble(hj));
                stampCardOrderMx.setHj(Double.parseDouble(hj));
                stampCardOrderMx.setState("1");
                stampCardOrderMx.setCzrq(new Date());
                stampCardOrderMxMapper.insert(stampCardOrderMx);
            }

            //更新价格、运费
            String yf1="0";
            if (Double.parseDouble(bysl)>zsl)    //总数量没有超过包邮数量，则需要收运费
            {
                yf1=yf;
            }else{
                yf1="0";
            }
            stampCardOrder.setJg(Double.parseDouble(jg));
            stampCardOrder.setYf(Double.parseDouble(yf1));
            stampCardOrder.setZjg(Double.parseDouble(jg)+Double.parseDouble(yf1));
            stampCardOrderMapper.updateById(stampCardOrder);

            infoMap.put("id",ToolBean.getEncode(stampCardOrder.getId()));
            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo orderInfo(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String id="";
            Map<String,Object> infoMap = new HashMap<>();
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (StringUtils.isEmpty(id)){
                return new ResultInfo(500,"订单信息丢失！",null);
            }else{
                try {
                    id =Url.decrypt(id);
                } catch (Exception e) {
                    return new ResultInfo(500,"订单信息丢失！",null);
                }
            }
            ToolBean.info("准备显示订单信息："+id);

            QueryWrapper<TWxAddressBook> queryWrapper = new QueryWrapper<>();
            TWxAddressBook receiverAddr = new TWxAddressBook(); //收件人默认地址
            queryWrapper = new QueryWrapper<>();
            receiverAddr = tWxAddressBookDao.getTWxAddressBook(queryWrapper.eq("hyxx_lsh",loginId).eq("ADDR_TYPE","2").eq("IS_DEFAULT","1").last("and rownum=1"));
            //默认地址
            infoMap.put("receiverAddr",receiverAddr);

            StampCardOrder stampCardOrder = stampCardOrderMapper.selectById(id);
            if (!stampCardOrder.getTHyxxId().equals(Long.parseLong(loginId))){
                return new ResultInfo(500,"不能查看非自己的订单信息！",null);
            }
            infoMap.put("id",stampCardOrder.getId());
            infoMap.put("stampCardOrder",stampCardOrder);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo orderQr(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String id="",yw="",ywcs="",dz="";
            Map<String,Object> infoMap = new HashMap<>();
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("yw")) {
                yw = jsonObject.getString("yw");
            }
            if (jsonObject.containsKey("ywcs")) {
                ywcs = jsonObject.getString("ywcs");
            }
            if (jsonObject.containsKey("dz")) {
                dz = jsonObject.getString("dz");
            }
            if (StringUtils.isEmpty(id)){
                return new ResultInfo(500,"订单信息丢失！",null);
            }else{
                try {
                    id =Url.decrypt(id);
                } catch (Exception e) {
                    return new ResultInfo(500,"订单信息丢失！",null);
                }
            }
            ToolBean.info("准备确认订单信息："+id);
            StampCardOrder stampCardOrder = stampCardOrderMapper.selectById(id);

            if (!stampCardOrder.getTHyxxId().equals(Long.parseLong(loginId))){
                return new ResultInfo(500,"禁止操作！",null);
            }
            if (StringUtils.isEmpty(yw) || StringUtils.isEmpty(ywcs)){
                return new ResultInfo(500,"业务参数丢失！",null);
            }
            if (StringUtils.isEmpty(dz)){
                return new ResultInfo(500,"地址信息丢失！",null);
            }
            if (!"0".equals(dz)){
                TWxAddressBook tWxAddressBook = tWxAddressBookDao.getTWxAddressBookById(Long.parseLong(dz));
                stampCardOrder.setSheng(tWxAddressBook.getTWxProvince().getName());
                stampCardOrder.setShi(tWxAddressBook.getTWxCity().getName());
                stampCardOrder.setQu(tWxAddressBook.getTWxCounty().getName());
                stampCardOrder.setLxdh(tWxAddressBook.getTel());
                stampCardOrder.setLxr(tWxAddressBook.getName());
                stampCardOrder.setDz(tWxAddressBook.getAddress());
                stampCardOrder.setLxdhJsm(tWxAddressBook.getTelJsm());
                stampCardOrder.setFullDz(tWxAddressBook.getFullAddress());
            }

            if (yw.equals("1")) {
                if (ywcs.equals("1")) {
                    //实际费用去掉邮费
                    stampCardOrder.setActFee(stampCardOrder.getJg());
                    stampCardOrderMapper.updateById(stampCardOrder);

                } else if (ywcs.equals("2")) {
                    stampCardOrder.setLqfs("2");
                    stampCardOrder.setActFee(stampCardOrder.getZjg());
                    stampCardOrderMapper.updateById(stampCardOrder);
                }
            } else {
            }

            infoMap.put("id",ToolBean.getEncode(stampCardOrder.getId()));
            infoMap.put("yw",yw);
            infoMap.put("ywcs",ywcs);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo addrQuery(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String lx="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if (StringUtils.isEmpty(lx)) lx="2";

            Map<String,Object> infoMap = new HashMap<>();
            IPage<TWxAddressBook> tWxAddressBookLPage =null; //地址列表,根据页面参数判断是寄件地址还是收件地址
            QueryWrapper<TWxAddressBook> queryWrapper = new QueryWrapper<>();
            Page<TWxAddressBook> page = new Page<>(1,16);
            if (lx.equals("2")) //收件地址
                tWxAddressBookLPage = tWxAddressBookDao.getTWxAddressBookLPage(page,queryWrapper.eq("HYXX_LSH",loginId).eq("ADDR_TYPE","2"));
            else    //寄件地址
                tWxAddressBookLPage = tWxAddressBookDao.getTWxAddressBookLPage(page,queryWrapper.eq("HYXX_LSH",loginId).eq("ADDR_TYPE","1"));
            List<TWxAddressBook> tWxAddressBookList = new ArrayList<>();
            tWxAddressBookList = tWxAddressBookLPage.getRecords();

            // 循环遍历 List 并改变每个对象的 age 属性
            for (TWxAddressBook tWxAddressBook : tWxAddressBookList) {
                tWxAddressBook.setDh(ToolBean.getDecode(tWxAddressBook.getTel()));
            }
            infoMap.put("tWxAddressBookL",tWxAddressBookList);
            infoMap.put("countTotal",tWxAddressBookLPage.getTotal());
            infoMap.put("pageCurrent",tWxAddressBookLPage.getCurrent());
            infoMap.put("pageTotal",tWxAddressBookLPage.getPages());
            infoMap.put("pageNext",tWxAddressBookLPage.getPages()-tWxAddressBookLPage.getCurrent());
            infoMap.put("lx",lx);
            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo addrArea(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            Map<String,Object> infoMap = new HashMap<>();
            String prov="",city="",county="";
            if (jsonObject.containsKey("prov")) {
                prov = jsonObject.getString("prov");
            }
            if (jsonObject.containsKey("city")) {
                city = jsonObject.getString("city");
            }
            if (jsonObject.containsKey("county")) {
                county = jsonObject.getString("county");
            }
            if (StringUtils.isEmpty(prov)){
                prov="17";
            }
            List<TWxProvince> tWxProvinceL= null;
            tWxProvinceL = tWxAddrDao.getProvL(Long.parseLong("1")); //获取省份列表
            infoMap.put("tWxProvinceL",tWxProvinceL);
            infoMap.put("prov",prov);
            infoMap.put("city",city);
            infoMap.put("county",county);
            infoMap.put("prov_name", tWxAddrDao.getProvName(prov));
            infoMap.put("city_name", tWxAddrDao.getCityName(city));
            infoMap.put("county_name", tWxAddrDao.getCountyName(county));

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Transactional
    public ResultInfo addrSave(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            Map<String,Object> infoMap = new HashMap<>();
            String lx ="";
            String name = "";
            String tel = "";
            String address = "";
            String prov_id = "";
            String city_id = "";
            String county_id = "";
            String defaultaddress = "";
            String grouplabel = "";
            String dh="";
            if (jsonObject.containsKey("lx")) {
                lx = jsonObject.getString("lx");
            }
            if (jsonObject.containsKey("name")) {
                name = jsonObject.getString("name");
            }
            if (jsonObject.containsKey("tel")) {
                tel = jsonObject.getString("tel");
                dh = tel;
            }
            if (jsonObject.containsKey("address")) {
                address = jsonObject.getString("address");
            }
            if (jsonObject.containsKey("prov_id")) {
                prov_id = jsonObject.getString("prov_id");
            }
            if (jsonObject.containsKey("city_id")) {
                city_id = jsonObject.getString("city_id");
            }
            if (jsonObject.containsKey("county_id")) {
                county_id = jsonObject.getString("county_id");
            }
            if (jsonObject.containsKey("defaultaddress")) {
                defaultaddress = jsonObject.getString("defaultaddress");
            }
            if (jsonObject.containsKey("grouplabel")) {
                grouplabel = jsonObject.getString("grouplabel");
            }
            if (StringUtils.isEmpty(prov_id) || StringUtils.isEmpty(city_id) || StringUtils.isEmpty(county_id) || StringUtils.isEmpty(address)){
                return new ResultInfo(500,"省市区和地址信息缺失！",null);
            }
            if (StringUtils.isEmpty(name) || StringUtils.isEmpty(tel)){
                return new ResultInfo(500,"姓名和联系电话缺失！",null);
            }

            String nonce_ms="";String nonce_zhi="";
            if (jsonObject.containsKey("nonce_ms")) {
                nonce_ms = jsonObject.getString("nonce_ms");
            }
            if (jsonObject.containsKey("nonce_zhi")) {
                nonce_zhi = jsonObject.getString("nonce_zhi");
            }

            //校验nonce
            ResultInfo ResultInfoCheckNonce = new ResultInfo();
            ResultInfoCheckNonce = tApiNonceH5Service.checkNonce(nonce_ms,loginId,nonce_zhi,"addr.save");

            if (ResultInfoCheckNonce.getCode()!=200)
            {
                return ResultInfoCheckNonce;
            }

            //如果选了默认，则需要更新所有为非默认
            if (defaultaddress.equals("1")) tWxAddressBookDao.updateDefault(Long.parseLong(loginId), lx);
            TWxAddressBook tWxAddressBook = new TWxAddressBook();
            tWxAddressBook.setAddrType(lx);
            tWxAddressBook.setAddress(address);
            tWxAddressBook.setGroupLabel(grouplabel);
            tWxAddressBook.setIsDefault(defaultaddress);
            tWxAddressBook.setName(name);
            if (!tel.equals("")) {
                if (tel.length() >= 4) {
                    String lastFourChars = tel.substring(tel.length() - 4);
                    tWxAddressBook.setTelJsm(lastFourChars);
                }
                try {
                    tel = Url.encrypt(tel);
                } catch (Exception e) {
                    throw new RuntimeException("电话号码加密发生错误！地址信息保存失败！");
                }
            }
            tWxAddressBook.setTel(tel);
            tWxAddressBook.setHyxxLsh(Long.parseLong(loginId));
            tWxAddressBook.setProvinceLsh(Long.parseLong(prov_id));
            tWxAddressBook.setCityLsh(Long.parseLong(city_id));
            tWxAddressBook.setCountyLsh(Long.parseLong(county_id));

            String full_address = tWxAddrDao.getProvName(prov_id)+tWxAddrDao.getCityName(city_id)+tWxAddrDao.getCountyName(county_id)+address;
            tWxAddressBook.setFullAddress(full_address);

            tWxAddressBookDao.insert(tWxAddressBook);
            infoMap.put("tWxAddressBook", tWxAddressBookDao.getTWxAddressBookById(tWxAddressBook.getLsh()));
            infoMap.put("dh", dh);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Transactional
    public String addrDefault(String loginId, String id,String lx) {
        QueryWrapper<TWxAddressBook> queryWrapper = new QueryWrapper<>();
        String rtn="";
        try {
            tWxAddressBookDao.updateDefault(Long.valueOf(loginId),lx);
            tWxAddressBookDao.setDefault("1",id,loginId);
            rtn="suc|";
        }catch (Exception e) {
            rtn = "err|"+e.getMessage();

        }finally {

            return rtn;
        }
    }

    public ResultInfo payInfo(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String id="",yw="",ywcs="";
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("yw")) {
                yw = jsonObject.getString("yw");
            }
            if (jsonObject.containsKey("ywcs")) {
                ywcs = jsonObject.getString("ywcs");
            }
            if (!StringUtils.isEmpty(id)) {
                try {
                    id = Url.decrypt(id);
                } catch (Exception e) {
                    return new ResultInfo(500,"订单信息缺失！",null);
                }
            }
            String tbname="stamp_card_order";
            String payPrice = tWxOrderDao.selectPrice(tbname, id);

            StampCardOrder stampCardOrder = new StampCardOrder();
            stampCardOrder = stampCardOrderMapper.getById(Long.parseLong(id));
            String hd = String.valueOf(stampCardOrder.getHdId());

            // 方法1：使用DecimalFormat
            DecimalFormat df = new DecimalFormat("0"); // 格式化为整数

            String  msg="活动名称："+stampCardOrder.getStampHd().getStampXm().getName()+"<br>";
            msg+="收件人："+stampCardOrder.getLxr()+"<br>";
            msg+="地址："+stampCardOrder.getDz()+"<br>";
            msg+="产品数量："+df.format(stampCardOrder.getZsl())+"<br>";
            msg+="支付费用："+payPrice+" 元<br>";
            Map<String,Object> infoMap = new HashMap<>();
            infoMap.put("id",id);
            infoMap.put("hd",hd);
            infoMap.put("xm",ToolBean.getEncode(stampCardOrder.getStampHd().getXmId()));
            infoMap.put("payPrice",payPrice);
            infoMap.put("msg",msg);
            infoMap.put("appid","wxc363f1f58277ae1f");
            infoMap.put("sffk",stampCardOrder.getIsfk());

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo getPayId(String loginId, Map<String, Object> map, HttpServletRequest httpServletRequest) throws ParseException {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String id="",yw="";
            String payPrice="";
            DecimalFormat df = new DecimalFormat("0.##");
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("yw")) {
                yw = jsonObject.getString("yw");
            }
            if (!StringUtils.isEmpty(id)) {
                try {
                    id = Url.decrypt(id);
                } catch (Exception e) {
                    return new ResultInfo(500,"订单信息缺失！",null);
                }
            }
            String tbname="stamp_card_order";
            String wx_id = stampCardOrderMapper.getWxIdByOrderId(id);
            wx_id="1";
            if (StringUtils.isEmpty(wx_id)) {
                return new ResultInfo(500,"未能获取活动支付配置参数！",null);
            }
            Long wxUserLsh = gettHyxxDao().selectById(loginId).getTWxUserLsh();
            if (null==wxUserLsh) {
                return new ResultInfo(500,"未能获取用户微信公众号信息！",null);
            }
            TWxInfo tWxInfo = tWxInfoDao.selectById(wx_id);
            if (null==tWxInfo) {
                return new ResultInfo(500,"未能获取微信公众号参数！",null);
            }
            TWxUser tWxUser = tWxUserDao.selectById(wxUserLsh);
            if (null==tWxUser) {
                return new ResultInfo(500,"未能获取用户微信公众号信息！",null);
            }
            //根据业务类型找到订单金额

            payPrice = tWxOrderDao.selectPrice(tbname,id);
            payPrice=String.valueOf(Double.parseDouble(payPrice)*100);
            ToolBean.info("金额*100："+payPrice);
            payPrice= String.valueOf(df.format(Double.parseDouble(payPrice)));
            ToolBean.info("金额*格式化："+payPrice);
            ToolBean.info("准备生成payid....");
            ToolBean.info("个性化邮票测试："+yw);
            ToolBean.info("id："+id);
            ToolBean.info("金额："+payPrice);

            //准备数据
            String timestamp = myDao.getTimestamp();

            String appid=tWxInfo.getAppid();
            String mch_id=tWxInfo.getMchId();
            String device_info="WEB";
            String nonce_str=ToolBean.getRandomString(30);
            String body="";
            //根据业务来写
            body = "2025个性化邮票订单["+id+"]";
            String detail="";
            String attach="";
            String out_trade_no= id+"-"+ToolBean.getRandomString(8);
            ToolBean.info("商户号为："+out_trade_no);
            String total_fee=payPrice;
            String spbill_create_ip=ToolBean.getIpAddress(httpServletRequest);
            String time_start= ToolBean.getTodayStr("yyyyMMddHHmmss");
            String time_expire=ToolBean.getNowTimeAddMillisecondStr(300000,"yyyyMMddHHmmss");//5分钟
            String goods_tag="";
            String notify_url= wdpz.getPaysuc();
            ToolBean.info("通知地址："+notify_url);
            String trade_type="JSAPI";
            String product_id="";
            String limit_pay="no_credit";
            String key= tWxInfo.getAppkey();
            String fee_type= "CNY";
            SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
            parameters.put("appid", appid);
            parameters.put("mch_id", mch_id);
            parameters.put("device_info", device_info);
            parameters.put("nonce_str", nonce_str);
            parameters.put("body", body);
            parameters.put("out_trade_no", out_trade_no);
            parameters.put("total_fee", total_fee);
            parameters.put("spbill_create_ip", spbill_create_ip);
            parameters.put("time_start", time_start);
            parameters.put("time_expire", time_expire);
            parameters.put("notify_url", notify_url);
            parameters.put("trade_type", trade_type);
            parameters.put("limit_pay", limit_pay);
            parameters.put("fee_type", fee_type);
            parameters.put("nonce_str", nonce_str);
            String openid = tWxUser.getOpenid();
            parameters.put("openid", openid);
            String sign=wxPay.createSign("UTF-8", parameters, key);

            String prepay_id =wxPay.createOrder(id,appid, mch_id, device_info, nonce_str, sign, body, detail, attach, out_trade_no, fee_type,total_fee, spbill_create_ip, time_start, time_expire, goods_tag, notify_url, trade_type, product_id, limit_pay, openid,tbname,tWxInfo);
            ToolBean.info("本次获取到的payid:"+prepay_id);
            //获取JS的签名
            SortedMap<Object,Object> js_parameters = new TreeMap<Object,Object>();
            js_parameters.put("appId", tWxInfo.getAppid());
            js_parameters.put("timeStamp", timestamp);
            js_parameters.put("nonceStr", nonce_str);
            js_parameters.put("package", "prepay_id="+prepay_id);
            js_parameters.put("signType", "MD5");

            String paySign = wxPay.createSign("UTF-8", js_parameters, key);
            ToolBean.info("准备返回payid...");
            String str = "suc|"+prepay_id+"|"+nonce_str+"|"+paySign+"|"+timestamp+"|";
            ToolBean.info("准备返回！"+str);
            Map<String,Object> infoMap = new HashMap<>();
            infoMap.put("appid",tWxInfo.getAppid());
            infoMap.put("prepay_id",prepay_id);
            infoMap.put("nonce_str",nonce_str);
            infoMap.put("paySign",paySign);
            infoMap.put("timestamp",timestamp);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public ResultInfo getPayIdDhm(String loginId, Map<String, Object> map, HttpServletRequest httpServletRequest) throws ParseException {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String id="",yw="",dhm="";
            String payPrice="";
            DecimalFormat df = new DecimalFormat("0.##");
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            if (jsonObject.containsKey("yw")) {
                yw = jsonObject.getString("yw");
            }
            if (jsonObject.containsKey("dhm")) {
                dhm = jsonObject.getString("dhm");
            }
            if (!StringUtils.isEmpty(id)) {
                try {
                    id = Url.decrypt(id);
                } catch (Exception e) {
                    return new ResultInfo(500,"订单信息缺失！",null);
                }
            }
            if (!StringUtils.isEmpty(dhm)) {
                try {
                    dhm = dhm.toUpperCase();
                } catch (Exception e) {
                    return new ResultInfo(500,"兑换码错误或者为空！",null);
                }
            }else{
                return new ResultInfo(500,"兑换码错误或者为空！",null);
            }

            ResultInfo resultInfo = stampOrderServices.dealDdxxByDhm(Long.parseLong(id),dhm);
            return backRtn(resultInfo,loginId);
        }
    }

    public String gxhTp1(String loginId,String imgDate1,String hdid) {
        try {
            ToolBean.info("gxh_tp1>>>活动ID："+hdid);
            try{
                hdid = Url.decrypt(java.net.URLDecoder.decode(hdid, "utf-8"));
            }catch (Exception e1){
                hdid = Url.decrypt(hdid);
            }

            if (StringUtils.isEmpty(loginId)){
                return "fail|合成图片1失败,用户信息丢失！";
            }

            if (StringUtils.isEmpty(hdid)){
                return "fail|合成图片1失败,活动信息丢失！";
            }

            //判断操作系统
            String os = System.getProperty("os.name");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String res = sdf.format(new Date());
            String newFileName = res + UUID.randomUUID().toString();

            String picPathReal="";
            if(os!=null&&os.contains("Windows")){
                //System.out.println("Windows");
                picPathReal="d:\\stamp\\card\\"+loginId+"\\";
            }else{
                //linux
                picPathReal="/home/stamp/card/"+loginId+"/";
            }

            File tempFile = new File(picPathReal);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }

            StampHd stampHd = stampHdMapper.selectById(hdid);

            //先保存上传图片
            String yhtp1 = picPathReal+newFileName+"_tp1.jpg";
            ToolBean.info("准备合成图片>>>保存用户上传图片1["+yhtp1+"]>>开始");
            FileUtil.base64ToImgTransformation(yhtp1,imgDate1);
            ToolBean.info("准备合成图片>>>保存用户上传图片1["+yhtp1+"]>>结束");

            //写订单信息
            StampCard stampCard = new StampCard();
            stampCard.setXmId(stampHd.getXmId());
            stampCard.setHdId(stampHd.getId());
            stampCard.setTHyxxId(Long.valueOf(loginId));
            stampCard.setLx("3");
            stampCard.setWjljTp1(yhtp1);
            stampCardMapper.insert(stampCard);

            return "{\"bz\":\"suc\",\"card_id\":\""+ToolBean.getEncode(stampCard.getId())+"\"}";


        } catch (Exception e) {
            e.printStackTrace();
            ToolBean.info("合成图片1失败["+e.toString()+"]");
            return "{\"bz\":\"fail\",\"card_id\":\"\"}";
        }
    }

    public String gxhTp2(String loginId,String imgDate2,String card_id) {
        try {
            ToolBean.info("gxh_tp2>>>cardud："+card_id);
            try{
                card_id = Url.decrypt(java.net.URLDecoder.decode(card_id, "utf-8"));
            }catch (Exception e1){
                card_id = Url.decrypt(card_id);
            }

            if (StringUtils.isEmpty(loginId)){
                return "fail|合成图片2失败,用户信息丢失！";
            }

            if (StringUtils.isEmpty(card_id)){
                return "fail|合成图片2失败,卡片信息丢失！";
            }

            //判断操作系统
            String os = System.getProperty("os.name");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String res = sdf.format(new Date());
            String newFileName = res + UUID.randomUUID().toString();

            String picPathReal="";
            if(os!=null&&os.contains("Windows")){
                //System.out.println("Windows");
                picPathReal="d:\\stamp\\card\\"+loginId+"\\";
            }else{
                //linux
                picPathReal="/home/stamp/card/"+loginId+"/";
            }

            File tempFile = new File(picPathReal);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }

            //先保存上传图片
            String yhtp2 = picPathReal+newFileName+"_tp2.jpg";
            ToolBean.info("准备合成图片>>>保存用户上传图片2["+yhtp2+"]>>开始");
            FileUtil.base64ToImgTransformation(yhtp2,imgDate2);
            ToolBean.info("准备合成图片>>>保存用户上传图片2["+yhtp2+"]>>结束");

            //写订单信息
            StampCard stampCard = new StampCard();
            stampCard = stampCardMapper.selectById(card_id);
            stampCard.setWjljTp2(yhtp2);
            stampCardMapper.updateById(stampCard);

            return "{\"bz\":\"suc\",\"card_id\":\""+ToolBean.getEncode(stampCard.getId())+"\"}";


        } catch (Exception e) {
            e.printStackTrace();
            ToolBean.info("合成图片2失败["+e.toString()+"]");
            return "{\"bz\":\"fail\",\"card_id\":\"\"}";
        }
    }

    public String qrta1(String loginId,String card_id) {
        try {
            ToolBean.info("qrta1>>>card_id："+card_id);
            try{
                card_id = Url.decrypt(java.net.URLDecoder.decode(card_id, "utf-8"));
            }catch (Exception e1){
                card_id = Url.decrypt(card_id);
            }

            if (StringUtils.isEmpty(loginId)){
                return "fail|合成图片失败,用户信息丢失！";
            }

            if (StringUtils.isEmpty(card_id)){
                return "fail|合成图片失败,卡片信息丢失！";
            }

            //判断操作系统
            String os = System.getProperty("os.name");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String res = sdf.format(new Date());
            String newFileName = res + UUID.randomUUID().toString();

            String picPathReal="";
            if(os!=null&&os.contains("Windows")){
                //System.out.println("Windows");
                picPathReal="d:\\stamp\\card\\"+loginId+"\\";
            }else{
                //linux
                picPathReal="/home/stamp/card/"+loginId+"/";
            }

            File tempFile = new File(picPathReal);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }

            StampCard stampCard = new StampCard();
            stampCard = stampCardMapper.getById(Long.parseLong(card_id));

            StampHd stampHd =stampCard.getStampHd();


            //找到个性化邮票模板，这里固定写死
            String mblj="";
            String mblj_dwt="";
            if(os!=null&&os.contains("Windows")){
                mblj="d:\\stamp\\hd\\"+stampHd.getId()+"\\"+stampHd.getGxhMb();
            }else{
                //linux
                mblj="/home/stamp/hd/"+stampHd.getId()+"/"+stampHd.getGxhMb();
            }
            if(os!=null&&os.contains("Windows")){
                //System.out.println("Windows");
                mblj_dwt="d:\\stamp\\hd\\"+stampHd.getId()+"\\"+stampHd.getDwt();
            }else{
                //linux
                mblj_dwt="/home/stamp/hd/"+stampHd.getId()+"/"+stampHd.getDwt();
            }
            //开始合成
            ToolBean.info("准备合成图片>>>");
            String hclj = picPathReal+newFileName+"_hctp.jpg";
            String hclj_zs = picPathReal+newFileName+"_hctp_zs.jpg";
            ToolBean.tc(stampCard.getWjljTp1(),stampCard.getWjljTp2(),mblj_dwt,hclj);

            //开始合成展示小图
            ToolBean.info("准备开始合成展示小图>>>hclj:"+hclj);
            ToolBean.info("准备开始合成展示小图>>>mblj:"+mblj);
            ToolBean.info("准备开始合成展示小图>>>hclj_zs:"+hclj_zs);
            FileUtil.hctp_gxhzs(hclj,mblj,hclj_zs);
            //写订单信息

            stampCard.setWjlj(hclj);
            stampCard.setWjljZs(hclj_zs);
            stampCardMapper.updateById(stampCard);

            return "{\"bz\":\"suc\",\"card_id\":\""+ToolBean.getEncode(stampCard.getId())+"\"}";


        } catch (Exception e) {
            e.printStackTrace();
            ToolBean.info("合成图片失败["+e.toString()+"]");
            return "{\"bz\":\"fail\",\"card_id\":\"\"}";
        }
    }

    public ResultInfo myorderQuery(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            Map<String,Object> infoMap = new HashMap<>();
            String dqym="",cxtj="",xm_id="";
            int cxym=1;
            if (jsonObject.containsKey("dqym")) {
                dqym = jsonObject.getString("dqym");
            }
            if (jsonObject.containsKey("cxtj")) {
                cxtj = jsonObject.getString("cxtj");
            }
            if (jsonObject.containsKey("xm_id")) {
                xm_id = jsonObject.getString("xm_id");
            }
            if (!StringUtils.isEmpty(xm_id)) {
                try {
                    xm_id = java.net.URLEncoder.encode(xm_id,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            }
            if (StringUtils.isEmpty(dqym)) cxym=1;
            else cxym = Integer.parseInt(dqym)+1;
            IPage<StampCardOrder> stampCardOrderIPage =null; //地址列表,根据页面参数判断是寄件地址还是收件地址
            QueryWrapper<StampCardOrder> queryWrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(cxtj)){
                //活动筛选
                queryWrapper.exists("select id from stamp_hd where name like '%"+cxtj+"%' and id = stamp_card_order.hd_id");
            }
            Page<StampCardOrder> page = new Page<>(cxym,10);
            stampCardOrderIPage = stampCardOrderMapper.getStampCardOrderLPage(page,queryWrapper.eq("t_hyxx_id",loginId).eq("scbz","0").orderByDesc("id"));
            List<StampCardOrder> stampCardOrderList = new ArrayList<>();
            stampCardOrderList = stampCardOrderIPage.getRecords();

            // 循环遍历 List 并改变每个对象的 age 属性
            for (StampCardOrder stampCardOrder : stampCardOrderList) {
                stampCardOrder.setUrlId(ToolBean.getEncode(stampCardOrder.getId()));
                stampCardOrder.setId(null);
            }
            infoMap.put("cxtj",cxtj);
            infoMap.put("xm_id",xm_id);
            infoMap.put("stampCardOrderList",stampCardOrderIPage.getRecords());
            infoMap.put("countTotal",stampCardOrderIPage.getTotal());   //总数
            infoMap.put("pageCurrent",stampCardOrderIPage.getCurrent());//当前页码
            infoMap.put("pageTotal",stampCardOrderIPage.getPages());
            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",infoMap);
            return backRtn(resultInfo,loginId);
        }
    }

    public String myorderDel(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return "fail|用户信息丢失！";
        }else{
            Map<String,Object> infoMap = new HashMap<>();
            String id="";
            int cxym=1;
            if (jsonObject.containsKey("id")) {
                id = jsonObject.getString("id");
            }
            try {
                ToolBean.info("myorder_del["+id+"]准备解密");
                id = Url.decrypt(java.net.URLDecoder.decode(id, "utf-8"));
                ToolBean.info("myorder_del["+id+"]解密完成");

                StampCardOrder stampCardOrder = stampCardOrderMapper.selectById(id);
                if (null!=stampCardOrder){
                    if (!stampCardOrder.getTHyxxId().equals(Long.parseLong(loginId))){
                        return "fail|没有找到订单信息！";
                    }
                    if ("1".equals(stampCardOrder.getIsfk())){
                        return "fail|已付款的订单无法删除！";
                    }
                    stampCardOrderMapper.updateScbz1(id);
                    return "suc";
                }else{
                    return "fail|没有找到订单信息！";
                }
            } catch (Exception e) {
                ToolBean.info("wdzp_del["+id+"]发生错误！"+e.toString());
                return "fail|删除异常！";
            }
        }
    }
}